package com.ss.android.article.base.feature.video.cdn.cdnOptimize;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import com.bytedance.article.common.a.g;
import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.feature.video.cdn.data.SSCdnCacheItem;
import com.ss.android.article.base.feature.video.cdn.data.SSCdnHostItem;
import com.ss.android.article.base.feature.video.cdn.data.SSCdnIpItem;
import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;

public class SSCdnTachometerTask extends AsyncTask {
    private static final String TAG = "CdnTachometer";
    private static final String TYPE_VIDEO_CDN = "video_cdn";
    private SSCdnCacheItem cacheItem;
    private WeakReference<ISSCdnTachometerCallback> weakRefCdnTachometer;

    public SSCdnTachometerTask(ISSCdnTachometerCallback arg2, SSCdnCacheItem arg3) {

        this.cacheItem = null;
        this.weakRefCdnTachometer = null;
        this.weakRefCdnTachometer = new WeakReference(arg2);
        this.cacheItem = arg3;
    }

    protected Boolean doInBackground(SSCdnCacheItem[] arg13) {
        int v1_1;
        InetAddress[] v6;
        Object v0;
        if(this.cacheItem != null && this.cacheItem.hostItemArrayList != null && this.cacheItem.hostItemArrayList.size() != 0) {
            Iterator v3 = this.cacheItem.hostItemArrayList.iterator();
            do {
            label_15:
                if(v3.hasNext()) {
                    v0 = v3.next();
                    if(TextUtils.isEmpty(((SSCdnHostItem)v0).cdnDomain)) {
                        continue;
                    }

                    Logger.d("CdnTachometer", "host: " + ((SSCdnHostItem)v0).cdnDomain);
                    ((SSCdnHostItem)v0).cdnIPArrayList = new ArrayList();
                    try {
                        long v4 = System.currentTimeMillis();
                        v6 = InetAddress.getAllByName(((SSCdnHostItem)v0).cdnDomain);
                        ((SSCdnHostItem)v0).cdnGetIPCostTime = System.currentTimeMillis() - v4;
                        if(v6 != null) {
                            int v4_1 = v6.length;
                            v1_1 = 0;
                            while(v1_1 < v4_1) {
                                InetAddress v5 = v6[v1_1];
                                SSCdnIpItem v7 = new SSCdnIpItem();
                                v7.ipHostName = ((SSCdnHostItem)v0).cdnDomain;
                                v7.ipAddress = v5.getHostAddress();
                                v7.ipCdnUrl = ((SSCdnHostItem)v0).cdnUrl;
                                SSCdnTachometerUtil.fetchVideoResponseData(v7);
                                Log.d("CdnTachometer", "  " + v7.ipAddress + " : " + v7.ipCostTime);
                                v7.setIpRank();
                                ((SSCdnHostItem)v0).cdnIPArrayList.add(v7);
                                ++v1_1;
                            }
                        } else {

                        }
                    } catch(UnknownHostException v1) {
                        v1.printStackTrace();
                    }

                    ((SSCdnHostItem)v0).setHostRank();
                    continue;
                } else {
                    Boolean v0_1 = Boolean.valueOf(true);
                    return v0_1;
                }
            }
            while(true);
        }

        return Boolean.valueOf(false);
    }

    protected Object doInBackground(Object[] arg2) {
        return this.doInBackground(((SSCdnCacheItem[])arg2));
    }

    protected void onPostExecute(Boolean arg3) {
        if(this.weakRefCdnTachometer != null && this.weakRefCdnTachometer.get() != null) {
            this.weakRefCdnTachometer.get().onTachometerFinish(arg3.booleanValue());
        }

        if((arg3.booleanValue()) && this.cacheItem != null && this.cacheItem.log() != null) {
            g.a("video_cdn", this.cacheItem.log());
        }
    }

    protected void onPostExecute(Object arg1) {
        this.onPostExecute(((Boolean)arg1));
    }
}

